import axios from 'axios'
import { getToken } from '@/utils/auth'
import { Notification, MessageBox, Message, Loading } from 'element-ui'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const requset = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL:  process.env.VUE_APP_BASE_API,
  // 超时
  timeout: 15000,
  withCredentials:true
})

// request拦截器
requset.interceptors.request.use(config => {
    if (getToken()) {
        config.headers['token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    }
  return config
}, error => {
    Promise.reject(error)
})

// 响应拦截器
requset.interceptors.response.use(res => {
    const result =  res.data
    const code = result.code || 200;
    if (code !== 200) {
        if(code === 9000){
            //location.href = '/login';
        }
        Message({
            message: result.message,
            type: 'error'
        })
        return Promise.reject(new Error(result.message))
    } else {
      return result.data
    }
  },
  error => {
    return Promise.reject(error)
  }
)


export default requset
